<template>
<div>
    <x-header>修改密码</x-header>
    <Group label-align="right" label-width="5em" label-margin-right="2em">
        <cell title="姓名：" value-align="left" :value="userInfo.fullname"></cell>
        <cell title="账号：" value-align="left" :value="userInfo.account"></cell>
        <x-input title="原密码：" placeholder="请输入原密码" v-model="oldPassword" required></x-input>
        <x-input title="新密码：" placeholder="请输入新密码" v-model="newPassword" :is-type="checkNew" required></x-input>
        <x-input title="确认密码：" placeholder="请输入确认密码" v-model="newPassword1" :is-type="checkNew1" required></x-input>
    </Group>
    <XButton :gradients="['#2d8cf0', '#5cadff']" class="myBtn" @click.native="btnClick">保存</XButton>
</div>
</template>
<script>
import { XInput, Group, XButton } from "vux";
export default {
  name: "homeEdit",
  components: {
    XInput,
    Group,
    XButton
  },
  data() {
    return {
      userInfo: null,
      oldPassword: null,
      newPassword: null,
      newPassword1: null
    };
  },
  created() {
    this.userInfo = this.$route.params.user;
  },
  methods: {
    checkNew(value) {
      if (this.newPassword1 && value != this.newPassword1) {
        return { valid: false, msg: "新密码需要跟确认密码一样！" };
      } else {
        return { valid: true };
      }
    },
    checkNew1(value) {
      if (this.newPassword1 && value != this.newPassword) {
        return { valid: false, msg: "确认密码需要跟新密码一样！" };
      } else {
        return { valid: true };
      }
    },
    btnClick() {
      if (!this.oldPassword && !this.newPassword && !this.newPassword1) {
        return this.$vux.toast.show({
          type: "warn",
          text: "请填写修改密码信息！"
        });
      }
      let _valid = this.checkNew(this.newPassword);
      let _valid1 = this.checkNew1(this.newPassword1);
      if (this.oldPassword && _valid.valid && _valid1.valid) {
        let defer = this.baseService.postForm(
          this.__ctx + "/org/user/updateUserPassWorld",
          {
            id: this.userInfo.id,
            oldPassword: this.oldPassword,
            newPassword: this.newPassword
          }
        );
        defer.then(data => {
          if (data.isOk) {
            let _this = this;
            this.$vux.toast.show({
              text: data.msg || "修改成功",
              onHide() {
                _this.$router.back(-1);
              }
            });
          } else {
            this.$vux.toast.show({
              type: "warn",
              text: data.msg
            });
          }
        });
      }
    }
  }
};
</script>

<style scoped lang="less">
.myBtn {
  margin-top: 5em;
  width: 80%;
  touch-action: none;
}
</style>
